#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Time    : 2021/6/27 11:36
# @Author  : Aaron
# @File    : generate_yml
# @Software: PyCharm
import os
import yaml
from selenium import webdriver
from calculator_pytest.setting import YML_DATA_PATH


def get_cookies() -> list:
    '''
    在cmd中输入chrome --remote-debugging-port=9222 启动浏览器
    登陆 https://work.weixin.qq.com/wework_admin/frame，扫描登录
    执行此文件，生成cookie，存放在https://work.weixin.qq.com/wework_admin/frame 中
    :return:list
    '''

    opt = webdriver.ChromeOptions()
    opt.debugger_address = "127.0.0.1:9222"
    driver = webdriver.Chrome(options=opt)
    driver.get("https://work.weixin.qq.com/wework_admin/frame")
    driver.find_element_by_id("menu_contacts").click()
    return driver.get_cookies()


def generate_cookie_yml():
    cookies = get_cookies()
    with open(os.path.join(YML_DATA_PATH, "enterprise_wechat_cookie.yml"), 'w', encoding="UTF-8") as f:
        yaml.safe_dump(cookies, f)


generate_cookie_yml()
